package sys

import (
	"github.com/gogf/gf/v2/net/ghttp"
	"seawolf-admin-go/internal/app/admin/controller"
	"seawolf-admin-go/internal/request/BaseReq"
	"seawolf-admin-go/internal/routers"
	"seawolf-admin-go/internal/service/AttachmentService"
	"seawolf-admin-go/utility/response"
)

type cAttachment struct {
	*controller.BaseController
}

func NewAttachmentController(group *ghttp.RouterGroup, defaultVersion string) {
	// 初始化内部变量
	inputReq := &BaseReq.I{}
	c := &cAttachment{&controller.BaseController{Req: inputReq}}
	c.New(group, defaultVersion)
}

func (c *cAttachment) New(group *ghttp.RouterGroup, defaultVersion string) {
	// 配置表名
	c.Req = &BaseReq.I{
		TableName:          "sw_attachment",
		InfoIgnoreProperty: "delete_at",
		SoftDeleteField:    "delete_at",
		ListQueryOp: &BaseReq.QueryOp{
			IsCache: 600,
		},
	}
	// 登录中间件
	// 权限判断中间件
	// 处理初始化方法和结束方法中间件
	group.Middleware(func(r *ghttp.Request) {
		c.Init(r)
		r.Middleware.Next()
		c.Shut(r)
	})
	// 注册基础路由
	controller.RegBase(group, c, nil)

	// UploadCheck 分片上传前检测
	group.GET("/uploadCheck", func(r *ghttp.Request) {
		routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
			"1.0.0": c.UploadCheck,
		}, defaultVersion, routers.DemoteStrategy)
	})
	// UploadChunk 分片上传
	group.POST("/uploadChunk", func(r *ghttp.Request) {
		routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
			"1.0.0": c.UploadChunk,
		}, defaultVersion, routers.DemoteStrategy)
	})
	// 合并分片
	group.POST("/mergeChunk", func(r *ghttp.Request) {
		routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
			"1.0.0": c.MergeChunk,
		}, defaultVersion, routers.DemoteStrategy)
	})
}

func (c *cAttachment) Page(r *ghttp.Request) *response.JsonResponse {
	return AttachmentService.Page(r, c.Req)
}

// UploadCheck 分片上传前检测
func (c *cAttachment) UploadCheck(r *ghttp.Request) *response.JsonResponse {
	return AttachmentService.UploadCheck(r)
}

// UploadChunk 分片上传
func (c *cAttachment) UploadChunk(r *ghttp.Request) *response.JsonResponse {
	return AttachmentService.UploadChunk(r)
}

// MergeChunk 合并分片
func (c *cAttachment) MergeChunk(r *ghttp.Request) *response.JsonResponse {
	return AttachmentService.MergeChunk(r)
}
